﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Service;
using PersonalHealth.Data.Model;
using PersonalHealth.Data.Data;
using PersonalHealth.Data.IService;
using System.Data.Entity;
using Core.UnitOfWork;
using Core.Repository;
using PersonalHealth.Data.Repository;
using System.Transactions;

namespace PersonalHealth.Data.Service
{
    public class UserService : Service<User, HealthContext>, IUserService
    {
        public void Register(User user)
        {
            using (HealthContext context = new HealthContext())
            {
                using (TransactionScope transaction = new TransactionScope())
                {
                    IUnitOfWork unitOfWork = new UnitOfWork(context);
                    IUserRepository rep = new UserRepository(unitOfWork);
                    int maxServiceNo = rep.GetMaxServiceNo();
                    user.ServiceNo = (maxServiceNo + 1).ToString("0000");
                    if (user is Doctor)
                    {
                        user.UserType = UserType.Doctor;
                    }

                    if (user is MeasureUser)
                    {
                        if (user.UserTypeId == 0)
                            user.UserType = UserType.MeasureUser;
                    }
                    rep.Add(user);

                    unitOfWork.Save();
                    transaction.Complete();
                }
            }
        }

        //public User Login(string name, string password)
        //{
        //    using (DbContext context = new HealthContext())
        //    {
        //        IUnitOfWork unitOfWork = new UnitOfWork(context);

        //        IUserRepository urep = new UserRepository(unitOfWork);
        //        var userList = urep.Find(c => c.UserName == name && c.Password == password);

        //        User user = null;
        //        if (userList != null && userList.Count > 0)
        //            user = userList[0];

        //        return user;
        //    }
        //}

        public User Login(string serviceNo, string password)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);

                IUserRepository urep = new UserRepository(unitOfWork);

                return urep.Login(serviceNo, password);
            }
        }



        public User LoginWithIcCard(string icCardNo)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);

                IUserRepository urep = new UserRepository(unitOfWork);
                return urep.LoginWithIcCard(icCardNo);
            }
        }

        public User LoginWithIdCard(string idCardNo)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);

                IUserRepository urep = new UserRepository(unitOfWork);
                return urep.LoginWithIdCard(idCardNo);
            }
        }


        public IList<Threshold> GetThreshold(string measureType, int userId)
        {
            using (DbContext context = new HealthContext())
            {
                IUnitOfWork unitOfWork = new UnitOfWork(context);
                IList<Threshold> list = new List<Threshold>();

                IPersonalDataThresholdRepository prep = new PersonalDataThresholdRepository(unitOfWork);
                var plist = prep.GetThreshold(measureType, userId);

                IOfficalDataThresholdRepository orep = new OfficalDataThresholdRepository(unitOfWork);
                var olist = orep.GetThreshold(measureType);

                IMeasureTypeRepository trep = new MeasureTypeRepository(unitOfWork);
                var type = trep.Find(c => c.Code == measureType).SingleOrDefault();
                if (type == null)
                    return list;

                IMeasureSubTypeRepository strep = new MeasureSubTypeRepository(unitOfWork);
                var subTypes = strep.Find(c => c.MeasureTypeId == type.Id);

                foreach (var subType in subTypes)
                {
                    var ps = plist.Keys.SingleOrDefault(c => c == subType.Code);
                    var os = olist.Keys.SingleOrDefault(c => c == subType.Code);

                    Threshold obj = new Threshold();
                    obj.MeasureSubType = subType;
                    if (!string.IsNullOrEmpty(ps))
                    {
                        obj.Min = plist[ps].Min;
                        obj.Max = plist[ps].Max;
                        obj.ExpectMin = plist[ps].ExpectMin;
                        obj.ExpectMax = plist[ps].ExpectMax;
                    }
                    else if (!string.IsNullOrEmpty(os))
                    {
                        obj.Min = olist[os].Min;
                        obj.Max = olist[os].Max;
                        obj.ExpectMin = 0;
                        obj.ExpectMax = 0;
                    }
                    list.Add(obj);
                }

                return list;
            }
        }
    }
}
